// 最后一次执行
function debounce(fn, wait) {
    let timer
    return function (...args) {
       const context = this 
       clearTimeout(timer)
       timer = setTimeout(()=>{
            fn.apply(context,args)
       },wait)
    }
}

// 第一次执行
function throttle(fn, wait) {
    let inThrottle
    return function (...args) {
        const context = this
        if(!inThrottle){
            fn.apply(context,args)
            inThrottle = true
            setTimeout(()=>inThrottle = false,wait)
        }
    }
}

// 不要用箭头函数，this取不到调用者
const throttle = (fn,wait=0) =>{
    let time
    return function(args){
        const that = this
        if(!time){
            fn.apply(that,args)
            time = true
            setTimeout(()=>{
                time = false
            },wait)
        }
    }
}

const debounce = (fn,wait=0) =>{
    let time
    return function (...args){
        const that = this
        cleanTimeout(time)
        time = setTimeout(()=>{
            fn.apply(that,args)
        },wait)
    }
}